<template>
  <view>
    <YgSearch />
    <YgTabs @change="changeTab" />
    <!-- <view v-if="current === 0" class="list">综合排序</view>
    <view v-if="current === 1" class="list">销量排序</view>
    <view v-if="current === 2" class="list">价格排序</view> -->
    <view class="goods-list">
      <navigator
        :url="`/pages/goods_detail/goods_detail?goods_id=${goods.goods_id}`"
        v-for="(goods, i) in goodsList"
        :key="i"
      >
        <view class="goods-item">
          <!-- 商品左侧图片区域 -->
          <view class="goods-item-left">
            <image
              :src="goods.goods_small_logo || defaultPic"
              class="goods-pic"
            ></image>
          </view>
          <!-- 商品右侧信息区域 -->
          <view class="goods-item-right">
            <!-- 商品标题 -->
            <view class="goods-name">{{ goods.goods_name }}</view>
            <view class="goods-info-box">
              <!-- 商品价格 -->
              <view class="goods-price"
                >￥{{ goods.goods_price | tofixed }}</view
              >
            </view>
          </view>
        </view>
      </navigator>
    </view>
  </view>
</template>

<script>
import { getGoodsListAPI } from "@/api/goods";
export default {
  data() {
    return {
      current: 0,
      // 请求参数对象
      queryObj: {
        // 查询关键词
        query: "",
        // 商品分类Id
        cid: "",
        // 页码值
        pagenum: 1,
        // 每页显示多少条数据
        pagesize: 10,
      },
      // 商品列表的数据
      goodsList: [],
      // 总数量，用来实现分页
      total: 0,
      // 是否正在请求数据
      isloading: false,
    };
  },
  filters: {
    // 把数字处理为带两位小数点的数字
    tofixed(num) {
      return Number(num).toFixed(2);
    },
  },
  onLoad(options) {
    // 将页面参数转存到 this.queryObj 对象中
    this.queryObj.query = options.query || "";
    this.queryObj.cid = options.cid || "";
    // 调用获取商品列表数据的方法
    this.getGoodsList();
  },
  methods: {
    changeTab(index) {
      this.current = index;
    },
    // 获取商品列表数据的方法
    async getGoodsList() {
      // ** 打开节流阀
      this.isloading = true;
      // 发起请求
      const { message } = await getGoodsListAPI(this.queryObj);
      console.log(message);
      // ** 关闭节流阀
      this.isloading = false;
      // if (message.meta.status !== 200) return uni.$showMsg();
      // 为数据赋值
      this.goodsList = [...this.goodsList, ...message.goods];
      this.total = message.total;
    },
  },
  // 触底的事件
  onReachBottom() {
    // 判断是否还有下一页数据
    if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total)
      return uni.$showMsg("数据加载完毕！");

    // 判断是否正在请求其它数据，如果是，则不发起额外的请求
    if (this.isloading) return;
    // 让页码值自增 +1
    this.queryObj.pagenum += 1;
    // 重新获取列表数据
    this.getGoodsList();
  },
  // 下拉刷新的事件
  onPullDownRefresh() {
    // 1. 重置关键数据
    this.queryObj.pagenum = 1;
    this.total = 0;
    this.isloading = false;
    this.goodsList = [];

    // 2. 重新发起请求
    this.getGoodsList(() => uni.stopPullDownRefresh());
  },
};
</script>

<style  lang="scss">
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;

  .goods-item-left {
    margin-right: 5px;

    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
    }

    .goods-price {
      font-size: 16px;
      color: #c00000;
    }
  }
}
</style>